﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADODB;

namespace TestReatXML
{
    internal class DBOperation
    {
        internal static void execute(String connString, String identity, DateTime time, DateTime end_time, Dictionary<int, string> values)
        {
            ADODB.Connection cn = new ADODB.Connection();
            try
            {
                cn.Open(connString, null, null, 0);
                cn.BeginTrans();
                ADODB.Command command = new ADODB.Command();
                command.ActiveConnection = cn;
                command.CommandText = "insert into logs(log_time,retrive_end) values(@log_time,@retrive_end)";
                command.Parameters.Refresh();
                command.Parameters["@log_time"].Value = time;
                command.Parameters["@retrive_end"].Value = end_time;
                
                object objRecAff;
                command.Execute(out objRecAff);
                
                ADODB.Recordset rs = new ADODB.Recordset();
                rs.Open(identity, cn);
                rs.MoveFirst();
                object id = rs.Fields[0].Value;
                rs.Close();
                
                //select id, log_id, code, text from logs_items
                
                command.CommandText = "insert into logs_items(log_id, code, text) values(@log_id, @code, @text)";
                command.Parameters.Refresh();
                foreach (KeyValuePair<int, string> item in values)
                {
                    command.Parameters["@log_id"].Value = Convert.ToInt32(id);
                    command.Parameters["@code"].Value = item.Key;
                    command.Parameters["@text"].Value = item.Value;
                    object oRA;
                    object oPRM = "Some string";
                    object rst = command.Execute(out oRA);
                    Console.WriteLine(rst);
                }              
                cn.CommitTrans();
            }
            catch (Exception ex)
            {
                try
                {
                    cn.RollbackTrans();
                }
                catch (Exception)
                {
                }
                throw ex;
            }
            finally
            {
                try
                {
                    cn.Close();
                }
                catch (Exception)
                {
                }
            }
        }

        internal static void validateConnectionString(String connString, String identity)
        {
            ADODB.Connection cn = new ADODB.Connection();
            try
            {
                cn.Open(connString, null, null, 0);
                ADODB.Recordset rs = new ADODB.Recordset();
                rs.Open("select id, log_time,retrive_end from logs", cn);
                if (!rs.EOF)
                    rs.MoveFirst();
                rs.Close();
                rs.Open("select id, log_id, code, text from logs_items", cn);
                if (!rs.EOF)
                    rs.MoveFirst();
                rs.Close();
                if (identity != null)
                {
                    rs.Open(identity, cn);
                    if (!rs.EOF)
                        rs.MoveFirst();
                    rs.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    cn.Close();
                }
                catch (Exception)
                {
                }
            }

        }
    }
}
